package com.leetcode.algorithm.y22.m06;

import java.util.HashSet;
import java.util.Set;

/**
 * 202. 快乐数
 * 
 * https://leetcode.cn/problems/happy-number/
 * 
 * @author jie.deng
 *
 */
class Question0202Solution01 {

	public boolean isHappy(int n) {
		Set<Integer> numSet = new HashSet<>();
		while (numSet.add(n)) {
			int sum = 0;
			while (n > 0) {
				int remain = n % 10;
				sum += remain * remain;
				n /= 10;
			}
			if (sum == 1) {
				return true;
			}
			n = sum;
		}

		return false;
	}

}